<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-13
 * Time: 下午6:07
 * To change this template use File | Settings | File Templates.
 */
class Model_Pay_Cft extends MY_Model
{
    /**
     * 支付渠道配置
     * @var array
     */
    private $conf = array();

    function __construct()
    {
        $this->conf['merchant_id'] = config_item('cft_merchant_id');   //商户号
        $this->conf['merchant_key'] = config_item('cft_merchant_key'); //密钥
        $this->conf['action_url'] = config_item('cft_request_url');  //支付接口地址
        $this->conf['payback'] = config_item('pay_back_url'); //支付完毕回调地址
    }

    /******************************************请求****************************************/
    public function request(array $arr)
    {
        $input['cmdno'] = 1; //财付通支付接口填1
        $input['date'] = date("Ymd");
        $input['bargainor_id'] = $this->conf['merchant_id']; //商户号
        $input['transaction_id'] = $this->conf['merchant_id'] . date("YmdHis") . mt_rand(1000,9999); //订单号
        $input['sp_billno'] = $arr['order_sn']; //内部订单号
        $input['total_fee'] = $arr['amount'] * 100; //单位分
        $input['fee_type'] = 1; //rmb
        $input['return_url'] = $this->conf['payback'];
        $input['attach'] = 'tomduiyi';
        $input['spbill_create_ip'] = $this->input->ip_address();

        $input['sign'] = $this->sign($input);

        $input['bank_type'] = 0;
        $input['desc'] = ""; //描述
        $input['cs'] = 'utf-8';
        $input['purchaser_id'] = ''; //买方财付通帐号
        ksort($input);
        return $this->makeRequest($this->conf['action_url'], $input);
    }

    /******************************************响应****************************************/
    function response()
    {
        $input['cmdno'] = $this->input->get_post('cmdno');
        $input['pay_result'] = $this->input->get_post('pay_result'); //0表示支付成功
        $input['date'] = $this->input->get_post('date');
        $input['transaction_id'] = $this->input->get_post('transaction_id');
        $input['sp_billno'] = $this->input->get_post('sp_billno');
        $input['total_fee'] = $this->input->get_post('total_fee');
        $input['fee_type'] = $this->input->get_post('fee_type');
        $input['attach'] = $this->input->get_post('attach');

        $sign = $this->input->get_post('sign');
        $my_sign = $this->sign($input);

        $rData = array(
            'result' => $input['pay_result'],
            'third_part_order_sn' => $input['transaction_id'],
            'amount' => $input['total_fee'],
            'order_sn' => $input['sp_billno'],
            'pay_user' => '',
            'pay_user_id' => '',
            'request_type' => $this->input->get_post('view') ? 1:2,
            'pay_channel' => '',
            'bank_order_id' => '',
            'pay_type' => 'cft',
            'response' => "<html><head>\r\n" .
                "<meta name=\"TENCENT_ONLINE_PAYMENT\" content=\"China TENCENT\">" .
                "<script language=\"javascript\">\r\n" .
                "window.location.href='{$this->conf['payback']}?view=1';\r\n" .
                "</script>\r\n" .
                "</head><body></body></html>",
        );

        //状态值(status) 1 成功， 2 签名错误， 3 订单支付失败
        $rData['status'] = 1;
        if (strtoupper($sign) !== $my_sign) {
            $rData['status'] = 2;
        }
        //判断支付状态
        if ($input['pay_result'] != '0') {
            $rData['status'] = 3;
        }
        return $rData;
    }

    private function makeRequest($api='' ,$data=array())
    {
        $form = "<form method=\"POST\" name=\"pay_form\" action=\"{$api}\">\n";
        foreach($data as $name=>$value)
        {
            $form .= "<input type=\"hidden\" name=\"{$name}\" value=\"{$value}\">\n";
        }
        $form .= "</form>\n";
        $form .= "<script>document.forms['pay_form'].submit()</script>";
        return $form;
    }

    private function sign($data)
    {
        $sign = '';
        foreach($data as $k=>$v)
        {
            $v && $sign .= "{$k}={$v}&";
        }
        $sign .= "key=".$this->conf['merchant_key'];
        return strtoupper(md5($sign));
    }
}
